<template>
	<view class="layout">
		<common-title></common-title>
	</view>
	<view class="tipinfo">
		<text>{{type}}展示,点击图片上传</text>
	</view>	
	<view class="picshow">
		<view class="landmarkinfo" v-if="balandmark_name.length">{{balandmark_name}}</view>
		<image :src=demopath mode="widthFix" @click="onClickPic"></image>
	</view>
<!-- 	<view class="poptip">
		<button @click="clickInfo">打开弹窗</button>
		<uni-popup ref="infoPopup" type="center">顶部弹出 Popup</uni-popup>
	</view> -->
</template>

<script setup>
import { ref } from 'vue';
import {onLoad,onUnload,onReachBottom,onShareAppMessage,onShareTimeline} from "@dcloudio/uni-app";
import { pathToBase64, base64ToPath } from 'image-tools'

const type = ref("算法")
const demopath = ref("https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/objdet_demo.png")
const file_base64 = ref("")
const obj = ref("")
const server_token = ref("db140af0-c1b8-11ee-b115-0242ac110003")
const server_url_api_name = ref("det")
const balandmark_name = ref("")
onLoad((e)=>{	
	type.value = e.type
	if (type.value === "物体检测"){
		server_url_api_name.value = "det"
	}else if(type.value === "人体姿态"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/posedemo.jpeg"
		server_url_api_name.value = "PoseFind"
	}else if (type.value === "手部姿态"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/handposedemo.png"
		server_url_api_name.value = "HandPose"
	}else if (type.value === "脸部姿态"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/facekt-demo.png"
		server_url_api_name.value = "FaceFind"
	}else if (type.value === "地标识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/landmark.png"
		server_url_api_name.value = "bdLandMark"
		balandmark_name.value = "天坛"
	}else if (type.value === "动物识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/panda.jpg"
		server_url_api_name.value = "bdAnimal"
		balandmark_name.value = "熊猫"
	}else if (type.value === "植物识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/mudan.jpg"
		server_url_api_name.value = "bdPlant"
		balandmark_name.value = "牡丹"
	}else if (type.value === "果蔬识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/guoshu.jpg"
		server_url_api_name.value = "bdGuoShu"
		balandmark_name.value = "西红柿"
	}else if (type.value === "菜品识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/mapodoufu.jpeg"
		server_url_api_name.value = "bdCaiPin"
		balandmark_name.value = "麻婆豆腐"
	}else if (type.value === "线稿出图"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/jianbihua.jpg"
		server_url_api_name.value = "bdLineSketch"
	}else if (type.value === "人物动漫"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/ani.jpeg"
		server_url_api_name.value = "bdAnime"
	}else if (type.value === "车辆识别"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/car.jpeg"
		server_url_api_name.value = "bdCarClassify"
		balandmark_name.value = "保时捷"
	}
	else if (type.value === "无人机检测"){
		demopath.value="https://aitestgo-bj.oss-cn-beijing.aliyuncs.com/docker/alg/uniapp/ui/unmanedplane.jpeg"
		server_url_api_name.value = "commonAlg"
		//balandmark_name.value = "无人机"
	}
	
})

function analysis_res(){
	//console.log(obj.value)
	if (type.value === "地标识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"]["landmark"]
		}		
	}else if(type.value === "动物识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"][0]["name"]
		}		
	}else if(type.value === "植物识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"][0]["name"]
		}		
	}else if(type.value === "果蔬识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"][0]["name"]
		}		
	}else if(type.value === "菜品识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"][0]["name"]
		}		
	}else if(type.value === "线稿出图"){
		
	}else if(type.value === "人物动漫"){
	
	}else if(type.value === "车辆识别"){
		if (obj.value.hasOwnProperty("result")){			
			balandmark_name.value = obj.value["result"][0]["name"]
		}		
	}
	
	

}

const onClickPic = async () => {
uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['camera', 'album'],
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						pathToBase64(tempFilePaths[0])
							.then(base64 => {
								//console.log(server_url_api_name.value)
								let base64String = base64.substring(base64.indexOf(',') + 1)
								//console.log('base64String', base64String)
								file_base64.value = base64String
								let request_data={}
								if (server_url_api_name.value==="commonAlg"){
									request_data["data"]=[										
										server_token.value,
										{
										"data":"data:image/jpeg;base64,"+base64String,
										"name":"p1.jpg",
										//"size":351604,
										},
										{
											"type":"unmannedplane"
										}
									]
								}
								else{
									request_data["data"]=[
										server_token.value,
										{
										"data":"data:image/jpeg;base64,"+base64String,
										"name":"p1.jpg",
										//"size":351604,
										}
									]
								}
								//console.log(server_url_api_name.value)
								uni.request({
									url:"https://rd.aitestgo.com/run/" + server_url_api_name.value,
									//url:"http://59.110.163.32:50052/run/" + server_url_api_name.value,
									//url:"http://172.16.75.68:9012/run/" + server_url_api_name.value,
									data:request_data,									
									method:"POST",
									success:res=>{
										//console.log(res)
										obj.value = res.data.data[0]
										if (obj.value.hasOwnProperty("imgUrl")){
											demopath.value=obj.value["imgUrl"]
										}
										else{
											demopath.value = tempFilePaths[0]
										}
										analysis_res()
										//let newShowHeight = res.data.data[0]["height"]*750/res.data.data[0]["width"]
										//imageStyle.height = `${newShowHeight}rpx`;
										//console.log("image_width:", res.data.data[0]["width"])
										//console.log("image_height:", res.data.data[0]["height"])										
									}
								})
							})
							.catch(error => {
								console.error(error)
							})
					}
				})
};
</script>

<style lang="scss" scoped>
.tipinfo{
	padding: 10px 0;
	background-color: #f2fff8;
	text{
		font-size:20px;
		font-weight: bold;
		display: flex;
		align-items: center;
		justify-content: center;
		
	}
}
.picshow{
	width:100%;
	.landmarkinfo{
		position: absolute;
		z-index: 2;
		top:16vh;
		left:1vh;
		background: rgba(0,0,0,0.3);
		font-size: 58rpx;
		border-radius: 30rpx;
		padding:8rpx 28rpx;
		backdrop-filter: blur(10rpx);
		color: #FFF;
	}
	image{
		width:100%;
	}
}
</style>
